import os
import sys
import constans
from PIL import Image, ImageDraw
from AddFrameApi import add_frame_to_image, get_inner_region_v2, get_watch_frame_inner_region, add_watch_frame_to_image


def resource_path(relative_path):
    """获取资源路径"""
    try:
        # 打包后的路径
        base_path = sys._MEIPASS
    except AttributeError:
        # 开发时的路径
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)


def crop_rounded_corners(image, radius=40):
    """
    将图像的四个角裁剪为圆角
    :param image: 输入的图像
    :param radius: 圆角的半径
    :return: 裁剪后的图像
    """
    # 创建一个与图像大小相同的透明掩码
    mask = Image.new("L", image.size, 0)
    draw = ImageDraw.Draw(mask)

    # 绘制一个圆角矩形作为掩码
    draw.rounded_rectangle((0, 0, image.width, image.height), radius, fill=255)

    # 将掩码应用到图像上
    result = image.copy()
    result.putalpha(mask)
    return result


def add_phone_frame(screenshot_path, frame_path, top_path, output_path, corner_radius=40):
    """
    将手机截图添加加框
    :param screenshot_path: 手机截图路径
    :param frame_path: 机框模板路径
    :param top_path: 顶部状态栏路径
    :param output_path: 输出图像路径
    :param corner_radius: 圆角半径
    :return: None
    """
    # 加载手机截图和机框模板
    screenshot = Image.open(screenshot_path).convert('RGBA')
    frame = Image.open(frame_path).convert('RGBA')
    # 机框宽高比
    frame_ratio = frame.width / frame.height

    # 调整手机截图的大小以适应机框模板
    screenshot = screenshot.resize((frame.width -22, frame.height - 22), Image.LANCZOS)
    if top_path is not None:
        top = Image.open(top_path).convert('RGBA')
        # 顶部状态栏宽高比
        top_ratio = top.width / top.height
        top = top.resize((frame.width - 24, int((frame.width - 24) / top_ratio)), Image.LANCZOS)

    # 裁剪手机截图的四个角为圆角
    screenshot = crop_rounded_corners(screenshot, radius=corner_radius)

    # 将手机截图粘贴到机框模板的最底层
    combined_image = Image.new('RGBA', frame.size)
    combined_image.paste(screenshot, (11, 11))  # 将截图至于底层
    if top_path is not None:
        combined_image.paste(top, (11, 15))  # 将截图置于底层
    combined_image.paste(frame, (0, 0), frame)  # 将机框至于上层

    # 保存处理后的图像
    combined_image.save(output_path)


def add_frame_v2(screenshot_path, frame_path, top_path, output_path, device):
    """
    将手机截图添加加框
    :param screenshot_path: 手机截图路径
    :param frame_path: 机框模板路径
    :param top_path: 顶部状态栏路径
    :param output_path: 输出图像路径
    :param device: 设备类型
    :return: None
    """
    # 加载机框模板
    frame = Image.open(frame_path).convert("RGBA")
    # 加载截图
    screenshot = Image.open(screenshot_path).convert('RGBA')
    if device.type == 'phone' and screenshot.width > screenshot.height:
        # screenshot = screenshot.transpose(Image.ROTATE_90)  # 将截图选择90°
        frame = frame.transpose(Image.ROTATE_90)  # 机框旋转90°
        device.DPI = (device.DPI[1], device.DPI[0])  # DPI高宽互换
    frame_inner_region = get_inner_region_v2(frame)
    combined_image = add_frame_to_image(screenshot, frame, top_path, device, frame_inner_region)
    combined_image.save(output_path)


def add_watch_frame(screenshot_path, frame_path, output_path):
    """
    将手表截图添加机框
    :param screenshot_path: 手表截图路径
    :param frame_path: 机框模板路径
    :param output_path: 输出图像路径
    :return: None
    """
    # 加载机框模板
    frame = Image.open(frame_path).convert("RGBA")
    # 加载截图
    screenshot = Image.open(screenshot_path).convert('RGBA')
    x, y, width, height = get_watch_frame_inner_region(frame)
    combined_image = add_watch_frame_to_image(screenshot, frame, x, y, width)
    combined_image.save(output_path)


def add_fold_frame(screenshot_path, frame_path, top_path, output_path, corner_radius=40):
    """
    将折叠屏截图添加机框
    :param screenshot_path: 手机截图路径
    :param frame_path: 机框模板路径
    :param top_path: 顶部状态栏路径
    :param output_path: 输出图像路径
    :param corner_radius: 圆角半径
    :return: None
    """
    # 加载机框模板
    frame = Image.open(frame_path).convert("RGBA")

    screenshot = Image.open(screenshot_path).convert('RGBA')
    combined_image = add_frame_to_image(screenshot, frame, top_path, corner_radius=40)
    combined_image.save(output_path)


if __name__ == '__main__':
    # add_frame_v2(file_path, frame_path, top_img, new_file_path, Phone)
    class Phone:
        CORNER_RADIUS = 45  # 圆角半径
        BOTTOM_Y_OFFSET = 5
        DPI = (496, 1026)
    add_frame_v2('test/screenshot_20250521_160157.jpg', 'resource/phone/机框.png','resource/phone/top_white.png', 'test/1.png', Phone)






















